﻿using System.Linq.Expressions;
using Basic.OADAL;
using Basic.OAModel.DB;
using Basic.OARemoteModel;
using Basic.OARemoteModel.DataFillIn.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.OACollect.lmpl
{
    internal class DataFillInCollect : IDataFillInCollect
    {
        private readonly IEmpDataFillInDAL _BasicDAL;

        public DataFillInCollect ( IEmpDataFillInDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public Result Get<Result> ( long id, Expression<Func<DBEmpDataFillIn, Result>> selector )
        {
            return this._BasicDAL.Get<Result>(id, selector);
        }
        public Result Get<Result> ( long id ) where Result : class
        {
            return this._BasicDAL.Get<Result>(id);
        }
        public DBEmpDataFillIn Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
        public Result[] Gets<Result> ( long[] ids ) where Result : class
        {
            return this._BasicDAL.Gets<Result>(ids);
        }
        public Result[] Gets<Result> ( long[] ids, Expression<Func<DBEmpDataFillIn, Result>> selector )
        {
            return this._BasicDAL.Gets<Result>(ids, selector);
        }
        public long Add ( FillInAdd data )
        {
            if ( this._BasicDAL.IsExists(a => a.CompanyId == data.CompanyId && a.Title == data.Title) )
            {
                throw new ErrorException("oa.fill.title.repeat");
            }
            return this._BasicDAL.Add(data);
        }
        public Result[] Query<Result> ( FillInQuery query, IBasicPage paging, out int count ) where Result : class
        {
            return this._BasicDAL.Query<Result>(query, paging, out count);
        }
        public bool Set ( DBEmpDataFillIn source, FillInSet data )
        {
            if ( source.Status != FillInStatus.起草 )
            {
                throw new ErrorException("oa.fill.not.allow.update");
            }
            if ( source.Title != data.Title && this._BasicDAL.IsExists(a => a.CompanyId == source.CompanyId && a.Title == data.Title) )
            {
                throw new ErrorException("oa.fill.title.repeat");
            }
            return this._BasicDAL.Update(source, data);
        }
        public void Delete ( DBEmpDataFillIn source )
        {
            if ( source.Status != FillInStatus.起草 )
            {
                throw new ErrorException("oa.fill.not.allow.delete");
            }
            this._BasicDAL.Delete(source.Id);
        }

        public bool Stop ( DBEmpDataFillIn source )
        {
            if ( source.Status == FillInStatus.停用 )
            {
                return false;
            }
            else if ( source.Status != FillInStatus.审核通过 )
            {
                throw new ErrorException("oa.fill.not.allow.stop");
            }
            this._BasicDAL.Stop(source.Id);
            return true;
        }
        public void SetAuditResult ( DBEmpDataFillIn source, FillInStatus status )
        {
            if ( source.Status != FillInStatus.审核中 )
            {
                throw new ErrorException("oa.fill.not.allow.set");
            }
            else if ( source.Status == status )
            {
                return;
            }
            this._BasicDAL.SetAuditResult(source.Id, status);
        }

        public void BeginAudit ( DBEmpDataFillIn db, int fillTotal )
        {
            if ( db.Status == FillInStatus.审核中 )
            {
                return;
            }
            else if ( db.Status != FillInStatus.起草 && db.Status != FillInStatus.审核未通过 )
            {
                throw new ErrorException("oa.fill.not.allow.set");
            }
            this._BasicDAL.SubmitAudit(db.Id, fillTotal);
        }
    }
}
